package cn.wolfcode.luowowo.member.server;

import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogistException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.domain.UserMessage;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.security.auth.login.LoginException;
import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper mapper;
    @Reference
    private IUserInfoCacheService service;

    /**
     * 查询单个
     * @param id 用户id
     * @return
     */
    @Override
    public UserInfo get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }


    /**
     * 手机号是否注册
     * @param phone
     * @return
     */
    @Override
    public boolean checkPhone(String phone) {
        return mapper.selectByPhone(phone)>0;
    }

    /**
     * 发送验证码
     * @param phone
     */
    @Override
    public void sendVerifyCode(String phone) {
        //创建验证码
        String code = UUID.randomUUID().toString().replace("-", "").substring(0, 4);
        //存入redis(唯一性)
        //String key = "VERIFY"+ phone;
        String key = RedisKeys.VERIFY.join(phone);
       service.sendVerifyCode(key,code,RedisKeys.VERIFY.getTime().intValue());
        //发送短信
        System.out.println("您注册的验证码是"+code+"请在"+ RedisKeys.VERIFY.getTime()+"分钟之内输入");
    }

    /**
     * 添加用户注册信息
     * @param verifyCode  验证码
     * @param phone 电话号码
     * @param nickname 昵称
     * @param password 密码
     * @param rpassword 第二次密码
     * @throws LoginException
     */
    @Override
    public void insert(String verifyCode, String phone, String nickname, String password, String rpassword)throws LoginException {
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"第二次密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码错误");
        AssertUtil.hasLength(phone,"手机不能为空");
        //判断两次密码是否相等
        AssertUtil.isEquals(password,rpassword,"两次密码不相等");
        String key = "Verify"+ phone;
        String code = service.getValueByKey(key);
        AssertUtil.isEquals(code,verifyCode,"两次验证码不相等");
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setState(UserInfo.STATE_DISABLE);
        userInfo.setLevel(1);
        userInfo.setPassword(password);
        userInfo.setPhone(phone);
        userInfo.setHeadImgUrl("static/images/default.jpg");
        mapper.insert(userInfo);
    }

    /**
     * 用户登录
     * @param username 用户名
     * @param password 密码
     * @return
     * @throws LoginException  自定义异常
     */
    @Override
    public String userLogin(String username, String password) throws LoginException{
        UserInfo user = mapper.selectByUsernameAndPassword(username,password);
        if (user == null){
            throw new LoginException("账号或密码错误");
        }
        String token = UUID.randomUUID().toString().replaceAll("-","");
        service.sendLoginUser(token,user);
        return token;
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<UserInfo> list() {
        return mapper.selectAll();
    }

    @Override
    public void update(UserInfo userInfo) {
        mapper.updateByPrimaryKey(userInfo);
    }

    @Override
    public void updatepassword(String phone, String password, String newpassword) throws LogistException{
        if (!StringUtils.hasLength(phone)){
            throw  new LogistException("手机号不允许为null");
        }
        if (!StringUtils.hasLength(password)){
            throw  new LogistException("密码不允许为null");
        }
        if (!StringUtils.hasLength(newpassword)){
            throw  new LogistException("新密码不允许为null");
        }
        UserInfo user = mapper.selectByUsernameAndPassword(phone,password);
        if (user == null){
            throw new LogistException("账号或密码错误");
        }
        mapper.updatepassword(phone,newpassword);
    }

    @Override
    public void verityOldPhone(String phone, String verifyCode) throws LogistException{
        AssertUtil.hasLength(verifyCode,"验证码错误");
        AssertUtil.hasLength(phone,"手机不能为空");
        String key = RedisKeys.VERIFY.join(phone);
        String code = service.getValueByKey(key);
        AssertUtil.isEquals(code,verifyCode,"验证码错误!");
       UserInfo userInfo = mapper.verityOldPhone(phone);
        if (userInfo == null){
            throw new LogistException("手机号错误");
        }
    }

    @Override
    public void updatePhone(Long id ,String phone, String verifyCode) throws LogistException {
        AssertUtil.hasLength(verifyCode,"验证码错误");
        AssertUtil.hasLength(phone,"手机不能为空");
        String key = RedisKeys.VERIFY.join(phone);
        String code = service.getValueByKey(key);
        AssertUtil.isEquals(code,verifyCode,"验证码错误");
        mapper.updatephone(id,phone);
    }

    @Override
    public void logout(Long id ,int state) {
        mapper.updatestate(id ,state);
    }

    @Override
    public UserInfo searchUser(String name) {
        return mapper.searchUser(name);
    }

    @Override
    public List<UserInfo> selectbreakUser(Long id) throws LogistException{
        return mapper.selectbreakUser(id);
    }

    @Override
    public void addbreakuser(Long bid, Long uid) throws LogistException {
        UserInfo userInfo =mapper.selectbreakuserByUid(bid ,uid);
        if (userInfo != null){
            throw  new LogistException("该用户已经拉黑了");
        }
        mapper.addbreakuser(bid,uid);
    }

    @Override
    public Long selectgold(Long id) {
        return mapper.selectgold(id);
    }

    @Override
    public List<UserInfo> queryByVistor(Long[] vistorIds) {
        return mapper.selectByVistor(vistorIds);
    }

    @Override
    public UserMessage getUserMessage(Long id) {
        return mapper.getUserMessage(id);
    }
}
